package com.sina.shop.controller.business;

import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.domain.business.Business;
import com.sina.shop.service.business.BusinessService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-04-12-16:58
 */
@Controller
@RequestMapping("/shop-web/business")
public class BusinessController extends BaseController {

    @Autowired
    private BusinessService businessService;

    @RequestMapping(value = "/updateBusiness", method = RequestMethod.POST)
    public ModelAndView updateBusiness(Business business) {
        LOG.info("business:{}", business);

        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            businessService.updateBusiness(business);
            result.put(CODE, OK);
            result.put(MSG, "更新店铺成功");
        } catch (UpdateException e) {
            LOG.info("更新店铺出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "/getBusinessListByAdminId", method = RequestMethod.GET)
    public ModelAndView getBusinessListByAdminId(@RequestParam("adminId") Long adminId) {
        LOG.info("adminId:{}", adminId);
        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> data = new HashMap<>(4);
        Map<String, Object> result = new HashMap<>(6);
        List<Business> businessList = businessService.getBusinessListByAdminId(adminId);

        data.put("businessList", businessList);
        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "/getBusinessList", method = RequestMethod.GET)
    public ModelAndView getBusinessList(Business business) {
        LOG.info("business:{}", business);
        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(6);
        Map<String, Object> map = businessService.getBusinessList(business);
        result.put(CODE, OK);
        result.put(DATA, map);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "/saveBusiness", method = RequestMethod.POST)
    public ModelAndView saveBusiness(Business business) {
        LOG.info("business:{}", business);
        business.setCreateTime(LocalDateTime.now());

        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            businessService.saveBusiness(business);
            result.put(CODE, OK);
            result.put(MSG, "新增商家成功");
        } catch (UpdateException e) {
            LOG.info("新增商家出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "/deleteBusiness", method = RequestMethod.POST)
    public ModelAndView deleteBusiness(@RequestParam("id") Long id) {
        LOG.info("id:{}", id);
        Map<String, Object> result = new HashMap<>(6);
        ModelAndView mv = new ModelAndView("commonView");

        try {
            businessService.deleteBusiness(id);
            result.put(CODE, OK);
            result.put(MSG, "删除商家成功");
        } catch (UpdateException e) {
            LOG.info("删除商家出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }
}
